<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$

class Page extends Think {
    // 起始行数
    public $firstRow	;
    // 列表每页显示行数
    public $listRows	;
    // 页数跳转时要带的参数
    public $parameter  ;
    // 分页总页面数
    protected $totalPages  ;
    // 总行数
    protected $totalRows  ;
    // 当前页数
    protected $nowPage    ;
    // 分页的栏的总页数
    protected $coolPages   ;
    // 分页栏每页显示的页数
    protected $rollPage   ;
    //状态筛选条件
    protected $code_type   ;
	// 分页显示定制
    protected $config = array('first'=>'<img style="width:14px;height:14px" title="第一页" src="__PUBLIC__/Images/resultset_first.png"/>','prev'=>'<img style="width:14px;height:14px"  title="上一页" src="__PUBLIC__/Images/resultset_previous.png"/>','next'=>'<img style="width:14px;height:14px"  title="下一页" src="__PUBLIC__/Images/resultset_next.png"/>','last'=>'<img style="width:14px;height:14px"  title="最后一页" src="__PUBLIC__/Images/resultset_last.png"/>','header'=>'笔记录 ','theme'=>'<table width="100%"><tr><td width="35%"> %first% %prePage% %listPage% %nextPage% %end% %radioBtn%</td><td align="right"><font size="2" color="#666633">共有</font><font size="2" color="#0000cc">%totalRow%</font><font size="2" color="#666633">%header%每页显示</font>%setrow%<font size="2" color="#666633">笔 当前第</font><font size="2" color="#0000cc">%nowPage%</font><font size="2" color="#666633">页 共</font><font size="2" color="#0000cc">%totalPage%</font><font size="2" color="#666633">页</font></td></tr></table>');
    //protected $config  =	array('first'=>'<table width="98%"><tr><td align="left" ><img title="第一页" src="../Public/images/resultset_first.png"/>','prev'=>'<img title="上一页" src="../Public/images/resultset_previous.png"/>','next'=>'<img title="下一页" src="../Public/images/resultset_next.png"/>','last'=>'<img title="最后一页" src="../Public/images/resultset_last.png"/></td><td align="right">','header'=>'笔记录','theme'=>' %upPage% %downPage% %first%  %prePage% %listPage%  %nextPage% %end% <font size="2" color="#666633">共有</font><font size="2" color="#0000cc">%totalRow%</font><font size="2" color="#666633">%header% 每页显示</font>%setrow%<font size="2" color="#666633">笔  当前第</font><font size="2" color="#0000cc">%nowPage%</font><font size="2" color="#666633">页  共</font><font size="2" color="#0000cc">%totalPage%</font><font size="2" color="#666633">页</font></td></tr></table>'); 
    /**
     +----------------------------------------------------------
     * 架构函数
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param array $totalRows  总的记录数
     * @param array $listRows  每页显示记录数
     * @param array $parameter  分页跳转的参数
     +----------------------------------------------------------
     */
    public function __construct($totalRows,$listRows,$code_type,$parameter='') {
        $this->code_type = $code_type;
        $this->totalRows = $totalRows;
        $this->parameter = $parameter;
        $this->rollPage = C('PAGE_ROLLPAGE');
        $this->listRows = !empty($listRows)?$listRows:C('PAGE_LISTROWS');
        $this->totalPages = ceil($this->totalRows/$this->listRows);     //总页数
        $this->coolPages  = ceil($this->totalPages/$this->rollPage);
        $this->nowPage  = !empty($_GET[C('VAR_PAGE')])?$_GET[C('VAR_PAGE')]:1;
        if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
            $this->nowPage = $this->totalPages;
        }
        $this->firstRow = $this->listRows*($this->nowPage-1);
    }

    public function setConfig($name,$value) {
        if(isset($this->config[$name])) {
            $this->config[$name]    =   $value;
        }
    }

    /**
     +----------------------------------------------------------
     * 分页显示输出
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     */
    public function show() {
//        if(0 == $this->totalRows) return '';
        $p = C('VAR_PAGE');
        $nowCoolPage      = ceil($this->nowPage/$this->rollPage);
        $url  =  $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;
        $parse = parse_url($url);
        if(isset($parse['query'])) {
            parse_str($parse['query'],$params);
            unset($params[$p]);
            $url   =  $parse['path'].'?'.http_build_query($params);
        }
        //上下翻页字符串
        $upRow   = $this->nowPage-1;
        $downRow = $this->nowPage+1;
        $theEndRow = $this->totalPages;
        $theFirstRow=1;
//        if ($upRow>0){
//            $upPage="<a href='".$url."&".$p."=$upRow'>".$this->config['prev']."</a>";
//        }else{
//            $upPage="";
//        }
//
//        if ($downRow <= $this->totalPages){
//            $downPage="<a href='".$url."&".$p."=$downRow'>".$this->config['next']."</a>";
//        }else{
//            $downPage="";
//        }
        // << < > >>
        if($this->nowPage == 1){
            $theFirst = "<a href='".$url."&".$p."=$theFirstRow' >".$this->config['first']."</a>";
            $prePage = "<a href='".$url."&".$p."=$theFirstRow'>".$this->config['prev']."</a>";
        }else{
            //$preRow =  $this->nowPage-1;
            $prePage = "<a href='".$url."&".$p."=$upRow' >".$this->config['prev']."</a>";
            $theFirst = "<a href='".$url."&".$p."=$theFirstRow' >".$this->config['first']."</a>";
        }
        if($this->nowPage == $this->totalPages){          	
            $nextPage = "<a href='".$url."&".$p."=$theEndRow'>".$this->config['next']."</a>";
            $theEnd = "<a href='".$url."&".$p."=$theEndRow' >".$this->config['last']."</a>";
        }else{
        	$nextPage ="<a href='".$url."&".$p."=$downRow'>".$this->config['next']."</a>";
            $theEnd="<a href='".$url."&".$p."=$theEndRow' >".$this->config['last']."</a>";
            //$nextRow = $this->nowPage+$this->rollPage;
            
        }
        
        
        //下拉分页选择
        $listPage="";
        //$tag="<if condition='this.value eq $this->nowPage'>selected</if>";
        for($i=1;$i<=$this->totalPages;$i++){
        	$options.="<option value='$i'> 第 ".$i." 页 </option>";
        	$listPage="<select id='sel' style='width:90px;background:#99ccff;height:20px;margin-top:3px' onchange='changlist(this.value,\"$url\")'>$options</select>";
        }
        
        //设定每页显示的记录数
        $setrow=
				"<select id='setrow' style='margin-left:1px;margin-top:1px;width:40px;background:#99ccff;' onchange='setrow(this.value)'>
        		<option value='5'>5</option><option value='10'>10</option><option value='15'>15</option><option value='20'>20</option><option value='25'>25</option>
        		</select>";
        
        //设定每页显示的记录数
//        $setrow="<div id='txtRows' style='position:absolute;width:1px;'>".
//        		"<span style='position:absolute;width:20px;overflow:hidden;'>"." 
//				<input id='selTotxt' maxlength='2' type='text' height='1px' width='20px' value='' onblur='setrow(this.value)'  onKeyUp='return webchat_chkkeysend(event,this.value)'></span></div><select id='setrow' style='margin-left:1px;margin-top:1px;width:40px;background:#99ccff;' onchange='setrow(this.value)'>
//        		<option value='5'>5</option><option value='10'>10</option><option value='15'>15</option><option value='20'>20</option><option value='25'>25</option>
//        		</select>";
        // 1 2 3 4 5
        $linkPage = "";
        for($i=1;$i<=$this->rollPage;$i++){
            $page=($nowCoolPage-1)*$this->rollPage+$i;
            if($page!=$this->nowPage){
                if($page<=$this->totalPages){
                    $linkPage .= "&nbsp;<a href='".$url."&".$p."=$page'>&nbsp;".$page."&nbsp;</a>";
                }else{
                    break;
                }
            }else{
                if($this->totalPages != 1){
                    $linkPage .= "&nbsp;<span class='current'>".$page."</span>";
                }
            }
        }
        $radioBtn="";
        $pageStr="";
        if($this->code_type!="")
        {
        	$codedtl=D("info_codedtl");
       	 	$result=$codedtl->where("code_type='".$this->code_type."' order by code_seq")->select();
        
        	$radioBtn="<td><table id='filtertb'>" .
        		"<tr  id='shenqing'>";
        		
        	foreach ( $result as $value ) {
       		$radioBtn.="<td><input name='rad'  type='radio'  value='".$value[code_no]."'  id='".$value[code_no]."'    onclick='GetRadioValuew(this.value,\"__URL__/index/adu/\")'/></td>
						<td><font size='2' color='#666633'>".$value[code_name]."<input  type='hidden'  name='valhid' value='".$value[code_no]."'/></font></td>";
			}
        	$radioBtn.="<td>" .
        		"<input  type='radio' name='rad'  value='qb'  id='qb'    onclick='GetRadioValuew(this.value,\"__URL__/index/adu/\")'/><td><font size='2'  color='#666633'>全部</font></td>" .
        		"</td></tr>".
        		"</table></td>";
        }
        
        
        
        $pageStr="";
        if($this->totalRows==0)
        {
        	$pageStr	 =	 str_replace(
            array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%listPage%','%nextPage%','%end%','%setrow%','%radioBtn%'),
            array('','','','','','','','','','','','',$radioBtn),'<table width="100%"><tr><td align="left">%first% %prePage% %listPage% %nextPage% %end% %radioBtn% </td><td align="right"><font size="2" color="#666633"></font><font size="2" color="#0000cc">%totalRow%</font><font size="2" color="#666633">%header%&nbsp;</font>%setrow%<font size="2" color="#666633"></font><font size="2" color="#0000cc">%nowPage%</font><font size="2" color="#666633"></font><font size="2" color="#0000cc">%totalPage%</font><font size="2" color="#666633"></font></td></tr></table>');
        }
        else
        {
        	$pageStr	 =	 str_replace(
            array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%listPage%','%nextPage%','%end%','%setrow%','%radioBtn%'),
            array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$downPage,$theFirst,$prePage,$listPage,$nextPage,$theEnd,$setrow,$radioBtn,),$this->config['theme']);
        }
        return $pageStr;
       
    }

}
?>